#!./bin/python
# -------------------------------------------------------------
# Build ExtJS index for Font Awesome glyphs
# -------------------------------------------------------------
# Copyright (C) 2007-2014, nocproject.org
# -------------------------------------------------------------

# Python modules
import re

CSS = "ui/pkg/fontawesome/css/font-awesome.min.css"
rx_content = re.compile(
    r"(?P<name>\.fa-[a-z0-9_\-]+:before(?:,\.fa-[a-z0-9_\-]+:before)*)\s*{\s*"
    r"content:\s*\"\\(?P<code>[a-f0-9]+)\"",
    re.MULTILINE | re.DOTALL)

def parse_css(path):
    with open(path) as f:
        data = f.read()
        data = data.replace(",\n", ",")
    code = {}
    for match in rx_content.finditer(data):
        names = match.group(1)
        v = "0x%s" % match.group(2)
        for name in names.split(","):
            if not name.startswith(".fa-"):
                continue
            name = name[4:-7]  # Strip .fa- and :before
            code[name] = v
            code[name.replace("-", "_")] = v
    r = [
        "//",
        "// Font Awesome glyph index",
        "// Built by ./scripts/fa-index",
        "// Do not change manually",
        "//",
        "Ext.namespace(\"NOC\");",
        "NOC.glyph = {"
    ]
    rr = [
        "    \"%s\": %s" % (g, code[g])
        for g in sorted(code)
    ]
    r += [",\n".join(rr)]
    r += ["};", ""]
    return "\n".join(r)

if __name__ == "__main__":
    print parse_css(CSS)
